//
//  NetworkTools.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/16.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import AFNetworking
private let weiboDomain = "com.baidu.data.error"

enum HttpMethod:String{
    
    case  Post = "POST"
    
    case  Get = "GET"
    
    
}
class NetworkTools: AFHTTPSessionManager {
    
    static let tools : NetworkTools = {
        
        let urlString = "http://www.api.weibo.com/"
        
        let url = NSURL(string: urlString)
        
        let tools = NetworkTools(baseURL: nil)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    func requestJsonDict(method:HttpMethod ,urlString:String,parameters: [String : AnyObject]?,finished:(result: [String : AnyObject]?,error:NSError?)->()){
        
        if method == HttpMethod.Get {
            
            GET(urlString, parameters: parameters, success: { (_, result) -> Void in
                
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                
                let dataError = NSError(domain: weiboDomain, code: -11111, userInfo: [NSLocalizedDescriptionKey: "数据模式不匹配"])
                
                finished(result: nil, error: dataError)
                }, failure: { (_, error) -> Void in
                    
                    finished(result: nil, error: error)
                    
            })
            
        }else{
            
            POST(urlString, parameters: parameters, success: { (_, result) -> Void in
                
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    
                    return
                }
                
                let dataError = NSError(domain: weiboDomain, code: -11111, userInfo: [NSLocalizedDescriptionKey : "数据模式不匹配"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    
                    finished(result: nil, error: error)
                    
            })
            
        }
        
        
    }
    //上传图片的方法
    func updateImage(urlString : String ,parameters:[String : AnyObject],imageData:NSData,finished:(result: [String : AnyObject]?,error:NSError?)->()){
        
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (binaryData) -> Void in
            
            binaryData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
            
            
            }, success: { (_, result) -> Void in
                
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    
                }
                
            }) { (_, error) -> Void in
                
                finished(result: nil, error: error)
        }
        
    }
    
    
    
    
}
